import React from 'react';
import PropTypes from 'prop-types';
import {Button, Form, Row, Col} from 'antd';
import {FORM_OPTIONS} from '../constants';
import {PERMISSION} from './constants';
import TextArea from 'Shared/components/TextArea';
import Export from '../common/Export';
import {injectIntl} from './intl';

const FormItem = Form.Item;

class QueryPanel extends React.PureComponent {
    handleFilterChange = (value, name) => {
        this.props.onConditionsChange(name, value);
    }
    handleFilterPressEnter = (value, name) => {
        this.props.onConditionsChange(name, value);
        this.props.querySearch();
    }
    render() {
        return (
            <Form className="form-standard">
                <Row>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem
                            label={this.props.intl.formatMessage({
                                id: 'queryPanel.form.dealerCode',
                                defaultMessage: '总代编号'
                            })}
                            {...FORM_OPTIONS.item}>
                            {this.props.isDealerUser
                                ? (<span>{this.props.enterprise.code}</span>)
                                : (<TextArea
                                    name="dealerCode"
                                    placeholder={this.props.intl.formatMessage({
                                        id: 'queryPanel.form.dealerCode.placeholder',
                                        defaultMessage: '最多输入1000个总代编号，以回车隔开'
                                    })}
                                    value={this.props.conditions.dealerCode}
                                    onBlur={this.handleFilterChange} />)
                            }
                        </FormItem>
                    </Col>
                </Row>
                <Row className="operation-buttons">
                    <Col span={6}>
                        <Button key="query" type="primary" onClick={this.props.querySearch} loading={this.props.loading}>
                            {this.props.intl.formatMessage({
                                id: 'queryPanel.btn.query',
                                defaultMessage: '查询'
                            })}
                        </Button>
                        <Button key="clear" onClick={this.props.onResetClick}>
                            {this.props.intl.formatMessage({
                                id: 'queryPanel.btn.reset',
                                defaultMessage: '重置'
                            })}
                        </Button>
                    </Col>
                    <Col span={18} className="col-align-right">
                        <Export key={PERMISSION.export} exportRequest={this.props.exportData} />
                    </Col>
                </Row>
               
            </Form>
        );
    }
}

QueryPanel.propTypes = {
    conditions: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    querySearch: PropTypes.func.isRequired,
    onConditionsChange: PropTypes.func.isRequired,
    onQueryDealerChange: PropTypes.func.isRequired,
    onResetClick: PropTypes.func.isRequired,
    exportData: PropTypes.func,
    intl: PropTypes.object,
    isDealerUser: PropTypes.bool,
    queryDealer: PropTypes.object,
};

import {connect} from 'react-redux';
import {
    saveQueryCondition,
    resetQueryPanel,
    querySearch,
    saveQueryDealer,
    exportData
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';

const getConditions = selectorFactory(['page', 'appState', 'queryCondition']);
const getQueryDealer = selectorFactory(['page', 'appState', 'queryDealer']);
const getInitEnterprise = selectorFactory(['page', 'domainData', 'initData', 'enterprise']);
const mapStateToProps = state => ({
    loading: state.getIn((['page', 'domainData', 'list', 'isFetching'])),
    enterprise: getInitEnterprise(state),
    isDealerUser: state.getIn((['page', 'domainData', 'initData', 'isDealerUser'])),
    conditions: getConditions(state),
    queryDealer: getQueryDealer(state),
});

const mapDispatchToProps = dispatch => ({
    onConditionsChange: (name, value) => dispatch(saveQueryCondition(name, value)),
    onQueryDealerChange: data => dispatch(saveQueryDealer(data)),
    querySearch: () => dispatch(querySearch()),
    exportData: () => dispatch(exportData()),
    onResetClick: () => dispatch(resetQueryPanel()),
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(QueryPanel));
